<template>
  <div>
    <div>
      <!-- <h3 v-show="!editDate">新增配置项：</h3> -->
      <el-form ref="form" :model="form">
        <el-form-item
          label="英文名："
          prop="configItem"
          :rules="[
            { required: true, message: '请输入英文名', trigger: 'blur' },
          ]"
        >
          <el-input v-model.trim="form.configItem" clearable />
        </el-form-item>
        <el-form-item
          label="配置值："
          prop="configValue"
          :rules="[
            { required: true, message: '请输入配置值', trigger: 'blur' },
          ]"
        >
          <el-input v-model.trim="form.configValue" clearable />
        </el-form-item>
        <el-form-item
          label="描述："
          prop="configDesc"
          :rules="[{ required: true, message: '请输入描述', trigger: 'blur' }]"
        >
          <el-input v-model.trim="form.configDesc" clearable />
        </el-form-item>
        <div class="classCenter">
          <el-button class="filter-item" type="primary" @click="handleSave"
            >保存</el-button
          >
          <el-button
            class="filter-item"
            style="margin-left: 10px"
            type="primary"
            v-show="editDate"
            @click="handlecancle(false)"
            >取消</el-button
          >
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import {
  configItemExist,
  jlcpCodeConfigSave,
  jlcpCodeConfigUpdate,
} from "@/api/lowcode";
export default {
  name: "addconfigurationComponents",
  components: {},
  props: ["editDate"],
  data() {
    return {
      form: {
        projectId: +sessionStorage.getItem("developLowCodeId"),
        configItem: "",
        configDesc: "",
        configValue: "",
        configType: "COMMON",
      },
    };
  },
  // created() {
  //   if (this.editDate) {
  //     this.form = JSON.parse(JSON.stringify(this.editDate));
  //   }
  // },
  //由于该组件添加了keep-alive缓存，所以每次进入这个组件调用activated生命周期
  activated() {
    if (this.editDate) {
      this.form = this.editDate;
    }
  },
  methods: {
    async handletemplateBlur(obj, name, title) {
      const res = await configItemExist(
        +sessionStorage.getItem("developLowCodeId"),
        obj[name]
      );
      if (res?.code === 200) {
        if (res.data) {
          this.$message({
            message: `${title}已存在，请从新输入`,
            type: "warning",
            showClose: true,
          });
          obj[name] = "";
          return true;
        }
      }
    },
    handlecancle(flag) {
      if (this.editDate) {
        this.$emit("close", flag);
      } else {
        this.$parent.$parent.$parent.handledTableBtnSave(
          "新增配置项",
          "通知配置列表",
          "notificationListComponents"
        );
      }
    },
    handleSave() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          let res = null;
          if (this.editDate) {
            if (this.editDate.configItem != this.form.configItem) {
              const flag = await this.handletemplateBlur(
                this.form,
                "configItem",
                "英文名"
              );
              if (flag) return;
            }
            res = await jlcpCodeConfigUpdate(this.form);
          } else {
            const flag = await this.handletemplateBlur(
              this.form,
              "configItem",
              "英文名"
            );
            if (flag) return;
            res = await jlcpCodeConfigSave(this.form);
          }
          if (res?.code === 200) {
            this.$message({
              message: "操作成功",
              type: "success",
            });
            this.handlecancle(true);
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "~@/styles/layout-table.scss";
$rowLength: 2;
@media all and (min-width: 1659px) {
  $rowLength: 2;
  @include calculation($rowLength: $rowLength);
}
@media all and (max-width: 1659px) {
  $rowLength: 3;
  @include calculation($rowLength: $rowLength);
}
.titleClass {
  margin-bottom: 10px;
  color: #5a5e66;
  font-weight: 700;
}
.bigbox {
  border: 1px solid #ccc;
  padding-top: 15px;
}
.classCenter {
  display: flex;
  justify-content: center;
}
.commonClass {
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
}
</style>
